﻿<%@ Page Language="C#" AutoEventWireup="true"  Debug="true" %>
<%@ Import Namespace="com.msn.search.soap" %>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="Microsoft.Security.Application" %>

<script language="C#" runat="server">
    void Page_Load(object sender, EventArgs e) {
        try {
            string key = Request.QueryString["searchKey"];
            if (!String.IsNullOrEmpty(key)) {
                string offset = Request.QueryString["searchOffset"];
                string count = Request.QueryString["searchCount"];
                CultureInfo ci = new CultureInfo("en-us");
                MSNSearchService s = new MSNSearchService();
                SearchRequest sr = new SearchRequest();
                SourceRequest[] srcr = new SourceRequest[1];
                srcr[0] = new SourceRequest();
                srcr[0].Source = SourceType.Web;
                if (!String.IsNullOrEmpty(count)) {
                    int iCount = Int32.Parse(count, ci);
                    if (iCount > 0 && iCount <= 50)
                        srcr[0].Count = iCount;
                    else
                        srcr[0].Count = 10;
                } 
                else
                    srcr[0].Count = 10;
                if (!String.IsNullOrEmpty(offset)) {
                    int iOffset = Int32.Parse(offset, ci);
                    if (iOffset > 0 && iOffset <= 250)
                        srcr[0].Offset = iOffset;
                    else
                        srcr[0].Offset = 0;
                } 
                else
                    srcr[0].Offset = 0;
                sr.Requests = srcr;
                sr.CultureInfo = "en-us";
                sr.Flags = SearchFlags.MarkQueryWords;
                sr.Query = Server.UrlDecode(key);
                sr.SafeSearch = SafeSearchOptions.Strict;
                
                // The recommended way to store application IDs are in your web.config file.
                // The code would look as follows:
                // sr.AppID = WebConfigurationManager.AppSettings.Get("appIDSearch");
                // This would look up the configuration defined as:
                //  <configuration>
	            //      <appSettings>
                //          <add key="appIDSearch" value="4C7A03F8DDBDD91F4EE428A59BFAB606E1DF372A" />
                //      </appSettings>
                //  </configuration>
                // Here though for convenience sake of where you can place this sample, the
                // application ID is hardcoded.
                sr.AppID = "4C7A03F8DDBDD91F4EE428A59BFAB606E1DF372A";
                
                SearchResponse srsp = s.Search(sr);
                PrintResults(srsp);
            }
        }
        catch (Exception fault) {
            Response.Write(fault.ToString());
        }
    }

    private void PrintResults(SearchResponse searchResponse)
    {
        StringBuilder sb = new StringBuilder(4096);
        CultureInfo ci = new CultureInfo("en-us");
        
        int iResponse = 0;
        foreach (SourceResponse sourceResponse in searchResponse.Responses) iResponse += sourceResponse.Total;
        sb.Append(Convert.ToString(iResponse, ci) + "#");
        foreach (SourceResponse sourceResponse in searchResponse.Responses) {
            Result[] sourceResults = sourceResponse.Results;
            if (sourceResponse.Total > 0) {
                foreach (Result sourceResult in sourceResults) {
                        if (!String.IsNullOrEmpty(sourceResult.Title)) 
                            sb.Append(
                                "<h5><a href=" + 
                                sourceResult.Url + 
                                ">" +
                                AntiXSSLibrary.HtmlEncode(sourceResult.Title) + 
                                "</a></h5>");
                        if (!String.IsNullOrEmpty(sourceResult.Description)) 
                            sb.Append(
                                "<p>" +
                                AntiXSSLibrary.HtmlEncode(sourceResult.Description) + 
                                "</p>");
                        if (!String.IsNullOrEmpty(sourceResult.DisplayUrl)) 
                            sb.Append(
                                "<p>" +
                                AntiXSSLibrary.HtmlEncode(sourceResult.DisplayUrl) + 
                                "</p>");
                        if (!String.IsNullOrEmpty(sourceResult.Url)) 
                            sb.Append(
                                "<p>" +
                                AntiXSSLibrary.HtmlEncode(sourceResult.Url) + 
                                "</p>");
                        if (!String.IsNullOrEmpty(sourceResult.SearchTags)) 
                            sb.Append(
                                "<p>SearchTags: " +
                                AntiXSSLibrary.HtmlEncode(sourceResult.SearchTags) + 
                                "</p>");
                        if (!String.IsNullOrEmpty(sourceResult.CacheUrl)) 
                            sb.Append(
                                "<p>CacheUrl: <a href=\"" + 
                                sourceResult.CacheUrl + 
                                "\">" +
                                AntiXSSLibrary.HtmlEncode(sourceResult.CacheUrl) + 
                                "</a></p>");
                }
            }
        }
        
        sb.Replace("&#57344;", "<strong>");
        sb.Replace("&#57345;", "</strong>");
        this.Response.Write(sb.ToString());
    }
</script>
